import http from '../../apis/api.js';
import util from '../../utils/util.js'
const app = getApp();
let self;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    acNo:'',
    custNo:'',
    activityInfo:{},
    userImage:null,
    swiperConf: {
      autoplay: true,
      interval: 3000,
      circular: true,
      current: 0,
      showCurrent: 1,
    },
    selectVoucher:{}
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    self = this;
    self.data.acNo = options.acNo;
    util.getUserInfo().then(res => {
      self.data.userImage = res.userInfo.avatarUrl;
      self.setData({
        userImage: self.data.userImage
      })
    })
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
 
    self.getactivityInfo();
  },
  getactivityInfo(){/**获取活动详情 */
    wx.getStorage({
      key: 'loginInfo',
      success: function (loginInfo) {
        self.data.custNo = loginInfo.data.custNo;
        http.getCustAcNum({
          acNo: self.data.acNo,
          custNo: self.data.custNo
        }).then(acNum => {
          http.getActivityInfo({ acNo: self.data.acNo }).then(res => {
            self.data.activityInfo = res.data;
            self.data.activityInfo.picList = [];
            self.data.activityInfo.picList.push(res.data.acLogo);
            self.data.activityInfo.prodStartTimeFormat = util.formatTime(res.data.startTime, 'YYYY.MM.DD');
            self.data.activityInfo.prodEndTimeFormat = util.formatTime(res.data.endTime, 'YYYY.MM.DD');
           
            self.data.activityInfo.overdue = self.data.activityInfo.endTime > new Date().getTime() ? true : false;
            if (self.data.activityInfo.prods.length>0){
              self.data.selectVoucher = self.data.activityInfo.prods[0];
            }
            
            self.data.activityInfo.contentFormat = self.data.activityInfo.content.replace(/\<img/gi, '<img style="width:100%;height:auto;display:block;margin: 0 auto;"');
            self.setData({
              activityInfo: self.data.activityInfo,
              selectVoucher: self.data.selectVoucher
            })
          })
        })
      }
    })
   
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return {
      url: self.data.activityInfo.acLogo,
      title: self.data.activityInfo.title,
      path: `/pages/activityDetail/activityDetail?acNo=${self.data.acNo}`
    }
  },
  radioChange(e) {
    let value = e.detail.value;
    self.data.activityInfo.prods.forEach(el => {
      if (el.id == value) {
        self.data.selectVoucher = el;
      }
    })
    self.setData({
      selectVoucher: self.data.selectVoucher
    })
  },
  bugGet(){
    self.se
    wx.showLoading({
      title: `${self.data.selectVoucher.saleAmt>0?'购买中':'领取中'}`,
    });
    wx.getStorage({
      key: 'openid',
      success: function(res) {
        http.buyAcProd({
          acNo: self.data.acNo,//活动编码
          buyNum: 1,//购买数量
          custNo: self.data.custNo,//客户编码
          openId:res.data, //微信open_id
          prodNo: self.data.selectVoucher.prodNo
        }).then(result => {
          wx.hideLoading();
          if (result.data.acInType =='51300101'){
            wx.showModal({
              title: '提示',
              content: '是否立即支付',
              success(pay) {
                if (pay.confirm) {
                  let wxParams = {
                    timeStamp: result.data.weChatAppletOrde.timeStamp,
                    nonceStr: result.data.weChatAppletOrde.nonceStr,
                    package: result.data.weChatAppletOrde.pay_package,
                    signType: result.data.weChatAppletOrde.signType,
                    paySign: result.data.weChatAppletOrde.paySign,
                  }
                  util.wxPay(wxParams).then(res => {
                    wx.navigateTo({
                      url: "/pages/vouchersList/vouchersList",
                    })
                  }, error => {
                    wx.navigateTo({
                      url: "/pages/orderList/orderList",
                    })
                  })
                }
              }
            })
          } else if (result.data.acInType == '51300100'){
            wx.showToast({
              title: '领取成功',
              icon: 'success',
              duration: 2000,
              success(){
                wx.navigateTo({
                  url: "/pages/vouchersList/vouchersList",
                })
              }
            })
          }
      
        })
      },
    })
  }
})